package com.ruoyi.common.utils.aspose;

import com.aspose.words.Document;
import com.aspose.words.ExportHeadersFootersMode;
import com.aspose.words.HtmlSaveOptions;
import com.aspose.words.SaveFormat;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 * @author Administrator
 */
public class DocWrapper implements FileWrapper {

    /**
     * doc/docx文档转换为html格式
     * @author ysz
     * @param multipartFile
     * @return java.util.Map
     **/
    @Override
    public FileContentEntity convertToHtmlContent(MultipartFile multipartFile) throws Exception{
        InputStream inputStream = multipartFile.getInputStream();
        ByteArrayOutputStream htmlStream = new ByteArrayOutputStream();
        Document document = new Document(inputStream);

        HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.HTML);
        saveOptions.setExportHeadersFootersMode(ExportHeadersFootersMode.NONE);
        saveOptions.setExportImagesAsBase64(true);
        saveOptions.setExportPageSetup(true);
        saveOptions.setPrettyFormat(true);
        saveOptions.setExportPageMargins(true);
        saveOptions.setExportListLabels(1);
        saveOptions.setExportDocumentProperties(true);
        saveOptions.setExportPageSetup(true);
        saveOptions.setExportTextInputFormFieldAsText(true);
        saveOptions.setExportTocPageNumbers(true);

        document.save(htmlStream, saveOptions);
        String htmlContent = findMatchesToLable(new String(htmlStream.toByteArray(),"UTF-8"));
        String fileName = FileUtils.getNameNotSuffix(multipartFile.getOriginalFilename());
        htmlStream.close();
        return new FileContentEntity(fileName, htmlContent);
    }

//    //查找所有图片集合
//    private static List<String> findMatchesToPic(String input) {
//        List<String> matches = new ArrayList<>();
//        Pattern pattern = Pattern.compile("<!--.*?-->");
//        Matcher matcher = pattern.matcher(input);
//        while (matcher.find()) {
//            String match = matcher.group();
//            matches.add(match);
//        }
//        return matches;
//    }

    //查找没用的标签，并替换
    private static String findMatchesToLable(String html) {
        Pattern pattern = Pattern.compile("<META.*?>", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(html);
        if (matcher.find()) {
            html = html.replace(matcher.group(), "");
        }

        Pattern patternMeta = Pattern.compile("<meta.*?>", Pattern.DOTALL);
        Matcher matcherMeta = patternMeta.matcher(html);
        if (matcherMeta.find()) {
            html = html.replace(matcherMeta.group(), "");
        }

        Pattern patternBody = Pattern.compile("<body.*?>", Pattern.DOTALL);
        Matcher matcherBody = patternBody.matcher(html);
        if (matcherBody.find()) {
            html = html.replace(matcherBody.group(), "");
        }

        Pattern patternStyle = Pattern.compile("<style.*?</style>", Pattern.DOTALL);
        Matcher matcherStyle = patternStyle.matcher(html);
        if (matcherStyle.find()) {
            html = html.replace(matcherStyle.group(), "");
        }

        html = html.replace("<html>", "");
        html = html.replace("<head>", "");
        html = html.replace("</head>", "");
        html = html.replace("</body>", "");
        html = html.replace("</html>", "");

        return html;
    }
}
